# blog/forms.py
from django import forms
from .models import Post, Category, Tag


class PostForm(forms.ModelForm):
    class Meta:
        model = Post
        fields = ['title', 'content', 'category', 'tags', 'is_published', 'image']
        widgets = {
            'title': forms.TextInput(attrs={
                'class': 'form-control',
                'placeholder': '输入文章标题',
                'id': 'post_title'  # 显式ID
            }),
            'content': forms.Textarea(attrs={
                'class': 'form-control',
                'rows': 10,
                'placeholder': '输入文章内容...',
                'id': 'post_content'  # 显式ID
            }),
            'category': forms.Select(attrs={
                'class': 'form-select',
                'id': 'post_category'  # 显式ID
            }),
            'tags': forms.SelectMultiple(attrs={
                'class': 'form-select select2-multiple',
                'id': 'post_tags',  # 显式ID
                'data-tags': 'true'
            }),
            'is_published': forms.CheckboxInput(attrs={
                'class': 'form-check-input',
                'id': 'post_published'  # 显式ID
            }),
        }

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # 确保ID设置
        for field_name, field in self.fields.items():
            if 'id' not in field.widget.attrs:
                field.widget.attrs['id'] = f'post_{field_name}'
form = PostForm(prefix='unique_prefix')

class PostListView:
    pass


class PostDetailView:
    pass


class PostCreateView:
    model = Post
    form_class = PostForm  # 使用我们自定义的表单类
    template_name = 'blog/post_form.html'

    def form_valid(self, form):
        form.instance.author = self.request.user
        return super().form_valid(form)

class PostUpdateView:
    model = Post
    form_class = PostForm
    template_name = 'blog/post_form.html'
class PostDeleteView:
    pass


def add_comment():
    return None